package com.bungarus.group.controller;

import com.bungarus.group.model.Group;
import com.bungarus.group.model.Member;
import com.bungarus.group.model.PagePayload;
import com.bungarus.group.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by tang on 2018/12/14.
 */
@RestController
public class MemberController extends BungarusController{
    private MemberService service;

    @Autowired
    public MemberController(MemberService service) {
        this.service = service;
    }

    @PostMapping("/tenants/{tenantId}")
    @ResponseStatus(HttpStatus.OK)
    public Member isExist(@PathVariable("tenantId")long tid,
                          @RequestParam("account")String account,
                          @RequestParam("pwd")String password) {

        return this.service.isExists(account, password, tid);
    }

    @PostMapping("/tenants/{tenantId}/members")
    @ResponseStatus(HttpStatus.OK)
    public Member createMember(@PathVariable("tenantId")long tid,
                               @RequestParam("account")String account,
                               @RequestParam("pwd")String password,
                               @RequestParam("id")long id,
                               @RequestParam("name")String name) {

        return this.service.createMember(id, tid, name, account, password);
    }

    @GetMapping(value = "/tenants/{tenantId}/groups", params = {"mid","offset","size"})
    public ResponseEntity<PagePayload<Group>> getGroupsMemberBelongsTo(@PathVariable("tenantId")long tid,
                                                   @RequestParam("mid")long mid,
                                                   @RequestParam("offset")int offset,
                                                   @RequestParam("size")int size) {

        return getGroupsContainMember(tid, mid, offset, size);
    }

    @GetMapping(value = "/tenants/{tenantId}/groups", params = {"mid"})
    public ResponseEntity<PagePayload<Group>> getGroupsMemberBelongsTo(@PathVariable("tenantId")long tid,
                                                                       @RequestParam("mid")long mid) {

        return getGroupsContainMember(tid, mid, MemberService.DEFAULT_START_PAGE, MemberService.DEFAULT_PAGE_SIZE);
    }

    private ResponseEntity<PagePayload<Group>> getGroupsContainMember(long tid, long mid, int offset, int size) {
        List<Group> groups = this.service.retrieveGroupsMemberBelongsTo(mid, tid);

        PagePayload<Group> result = new PagePayload<>();
        result.setPage(offset);
        result.setSize(size);
        result.setPageContents(groups);
        result.setTotalCount(this.service.totalCountOfGroupsMemberBelongsTo(mid, tid));

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .cacheControl(CacheControl.noCache())
                .body(result);
    }
}
